﻿using Newtonsoft.Json;
using Renci.SshNet;
using Renci.SshNet.Sftp;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using PenguinPublishNS.Models;

namespace PenguinPublishNS.Views
{

    public partial class HostRemoteDir : UserControl
    {
        public ICollectionView myView;
        public string pPubPath = PenguinPublish.dataPath;
        private HostServer curHost;
        private List<WebFile> listFileAndDirs = new List<WebFile>();

        IEnumerable<SftpFile> myList = new List<SftpFile>();
        IEnumerable<FileInfo> mySyncList = new List<FileInfo>();

        private ConnectionInfo sftpConnInfo;

        public HostRemoteDir()
        {
            InitializeComponent();
            if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
            {
                titleLabel.Text = "No Network Connection! form disabled";
                titleLabel.Foreground = System.Windows.Media.Brushes.Red;
                this.IsEnabled = false;
            }
            else
            {
                var comboStreamR = new StreamReader(pPubPath + @"\dataHostSetting.json");
                var comboResults = JsonConvert.DeserializeObject<List<HostServer>>(comboStreamR.ReadToEnd());

                //this.cboHostNames.DataContext = comboResults;
                //this.txtLastPublished.DataContext = comboResults;
                this.DataContext = comboResults;
                this.cboHostNames.ItemsSource = comboResults;

                myView = CollectionViewSource.GetDefaultView(comboResults);
               // curHost = (HostServer)myView.CurrentItem;   // since we fall out of the combobox selectionchanged event
               // initializeSFTP();
            }
        }

        private void btnList_Click(object sender, RoutedEventArgs e)
        {
            SftpClient sftp = new SftpClient(sftpConnInfo);
            sftp.Connect();
            myList = sftp.ListDirectory(curHost.sitePath);
            remDirGrid.DataContext = myList;
            remDirGrid.ItemsSource = myList;  // TODO: use LINQ to get just a couple of columns
        }

        private void btnCompare_Click(object sender, RoutedEventArgs e)
        {
            // a good place to check our local snapshot against the actual remote files
        }

        private void cboHostNames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var hostID = ((ComboBox)sender).SelectedValue;
            if (myView != null)
            {
                myView.MoveCurrentToPosition(((ComboBox)sender).SelectedIndex);
                curHost = (HostServer)myView.CurrentItem;
                curHost.ipAvailable = sftpUtils.chkHostIp(curHost.HostAddress);
                BindingOperations.GetBindingExpression(chkValidIP, CheckBox.IsCheckedProperty).UpdateTarget();
                if (curHost.ipAvailable)
                {
                    sftpConnInfo= sftpUtils.initializeSFTP(curHost.HostAddress, curHost.username, curHost.usePPK, curHost.password, curHost.keyPassphrase, curHost.privateKey);
                    gridButtons.IsEnabled = true;
                }
                else
                {
                    gridButtons.IsEnabled = false;
                }

            }

            try // hostID will be null as form is created
            {
                var streamR = new StreamReader(pPubPath + @"\" + hostID + @"\dataWebFile.json");
                var results = JsonConvert.DeserializeObject<List<WebFile>>(streamR.ReadToEnd());
                remDirGrid.DataContext = results;
                remDirGrid.ItemsSource = results;
            }
            catch { }
        }

        private void cboHostNames_Loaded(object sender, RoutedEventArgs e)
        {
            cboHostNames_SelectionChanged(sender, null);
        }


    }
}
